#!/bin/bash

start-test() {
    local ip=$(cat $TEST_DIR/databases/*/metadata/ip)
    local port=$(cat $TEST_DIR/databases/*/metadata/port)
    local pass=$(cat $TEST_DIR/databases/*/metadata/password)
    local image=$(cat $TEST_DIR/databases/*/metadata/image)
    # Starting with mariadb 11, you have to call to mariadb instead of mysql
    if [[ $image == *mariadb* ]]; then
       db_type="mariadb"
    else
       db_type="mysql"
    fi
    DB_CONNECTION_TEST="
        docker run --rm
        --name connection-test
        $image
        $db_type
          --host=$ip
          --port=$port
          --user=root
          --password=$pass
          --execute=status" \
    timeout --foreground 1m bash -c "wait-for-db-connection"
    KINE_IMAGE=$IMAGE KINE_ENDPOINT="mysql://root:$pass@tcp($ip:$port)/kine" provision-kine
    local kine_url=$(cat $TEST_DIR/kine/*/metadata/url)
    K3S_DATASTORE_ENDPOINT=$kine_url provision-cluster
}
export -f start-test

VERSION_LIST="\
    mysql 8.4
    mysql 8.0
    mariadb 11.4
    mariadb 10.11
    mariadb 10.6"

while read ENGINE VERSION; do
    LABEL=$ENGINE-$VERSION DB_PASSWORD_ENV=MYSQL_ROOT_PASSWORD DB_IMAGE=docker.io/library/$ENGINE:$VERSION run-test
done <<< $VERSION_LIST

